package com.itheima.servlet;

import com.itheima.domain.Student;
import com.itheima.service.impl.LogServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/webservlet")
public class StuServlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        try {
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            Student student = new Student();
            student.setName(username);
            student.setPassword(password);
            LogServiceImpl logService = new LogServiceImpl();
            Student student1 = logService.LogServiceStudent(student);
            if (student1!=null){
                resp.getWriter().write("登陆成功");
                req.getSession().setAttribute("user",student1);
                resp.sendRedirect(req.getContextPath()+"/serce.jsp");
            }else {
                req.setAttribute("errorMsg","帐号和密码错误");
                req.getRequestDispatcher("/login.jsp").forward(req,resp);
              /*  resp.getWriter().write("登陆失败，3秒后返回");
                resp.setHeader("Refresh", "3;URL=/login.jsp");*/
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
